﻿using ApiHelper;
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Windows;

namespace Client
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void btn1_Click(object sender, RoutedEventArgs e)
        {
            send();
        }

        public async void send()
        {
            try
            {

                string apiUrl = $"http://localhost:8009/Studentsys/StudentQuery/张三"; // 替换为实际的API接口URL

                // 创建HttpClient实例
                using (HttpClient client = new HttpClient())
                {
                    try
                    {
                        // 发送GET请求并获取响应
                        HttpResponseMessage response = await client.GetAsync(apiUrl);
                        response.EnsureSuccessStatusCode(); // 确保请求成功，否则抛出异常

                        // 读取响应内容
                        string responseBody = await response.Content.ReadAsStringAsync();
                        txbResult.Text = responseBody;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show($"访问{apiUrl}失败");
                    }
                }
            }
            catch (Exception EX)
            {

                throw;
            }
        }

        private void btn2_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string url = "http://localhost:8009/Studentsys/StudentQuery/StudentQueryByAge";
                string ss = HttpHelper.Post(url, "17");
                // https比http多了一个ssl的安全协议，普通的调用方法会触发“远程主机强迫关闭”报错，应该按照以下方法实现
                HttpWebRequest request = null;
                if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
                {
                    //这里设置了协议类型。
                    ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
                    ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
                    ServicePointManager.CheckCertificateRevocationList = false;
                    ServicePointManager.Expect100Continue = false;
                    ServicePointManager.DefaultConnectionLimit = 512;
                    request = WebRequest.Create(url) as HttpWebRequest;
                    request.ProtocolVersion = HttpVersion.Version10;
                }
                else
                {
                    request = (HttpWebRequest)WebRequest.Create(url);
                }
                request.Method = "POST";
                request.ContentType = "application/json";
                string data = "17";

                byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString());
                request.ContentLength = byteData.Length;

                using (Stream postStream = request.GetRequestStream())
                {
                    postStream.Write(byteData, 0, byteData.Length);
                }

                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {
                    StreamReader reader = new StreamReader(response.GetResponseStream());
                    txbResult.Text = reader.ReadToEnd();
                }
            }
            catch (Exception EX)
            {

                throw;
            }
        }
    }
}
